package client;

import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

import logic.Document;
import logic.Template;
import net.retrievers.Retriever;
import net.rmi.List;
import net.rmi.Logon;
import net.rmi.Reporter;

public class RmiFactory extends Factory {
	private Registry registry;
	private Retriever<Document> retriever;
	private List list;
	private Logon logon;
	private Reporter lookup;
	private Retriever<Template> retriever2;

	public RmiFactory() {
		try {
			registry = LocateRegistry.getRegistry(Config.getHost());
		} catch (RemoteException e) {
			throw new RuntimeException((e));
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Retriever<Document> getDocumentRetriever() {
		try {
			if (retriever == null)
				retriever = (Retriever<Document>) registry.lookup("r_logic.Document");
			return retriever;
		} catch (Exception e) {
			throw new RuntimeException((e));
		}

	}

	@Override
	public List getList() {
		try {
			if (list == null)
				list = (List) registry.lookup("list");
			return list;
		} catch (Exception e) {
			throw new RuntimeException((e));
		}
	}

	@Override
	public Logon getLogon() {
		try {
			if (logon == null)
				logon = (Logon) registry.lookup("logon");
			return logon;
		} catch (Exception e) {
			throw new RuntimeException((e));
		}
	}

	@Override
	public Reporter getReporter() {
		try {
			if (lookup == null)
				lookup = (Reporter) registry.lookup("reporter");
			return lookup;
		} catch (Exception e) {
			throw new RuntimeException((e));
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public Retriever<Template> getTemplateRetriever() {
		try {
			if (retriever2 == null)
				retriever2 = (Retriever<Template>) registry.lookup("r_logic.Template");
			return retriever2;
		} catch (Exception e) {
			throw new RuntimeException((e));
		}
	}

}
